import
myRequest
from '@/static/js/request.js';

// uni.login()封装
const wxLogin = function(openid) {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success(res) {
				if (res.code) {
					resolve(res.code)
				} else {
					reject(res.errMsg);
				}
			}

		})
	})
}
let resVo = {};
/*微信小程序登录*/
const wechatAppLogin = function() {
	/*登录提示*/
	uni.getUserProfile({
		desc: '获取用户授权',
		success: res => {
			let name = res.userInfo.nickName;
			let avatarUrl = res.userInfo.avatarUrl;
			let appid = "wx3287918578fbd645";
			let secret = "375dbe4a9f7b36e5f69a5b61bdd3fa5c";
			wxLogin().then(code => { // 引用uni.login()封装
			let url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' +
				secret + '&js_code=' + code;
				uni.request({
					url: url, // 请求路径
					success: result => {
						let openid = result.data.openid;
						resVo.openid = openid;
						resVo.name = name;
						resVo.avatarUrl = avatarUrl;
						myRequest({
							url: "/front/user/register",
							method: "post",
							data: JSON.stringify(resVo)
						})
					},
				});
			})
		}
	})
	return resVo;
	
}

module.exports = {
	wechatAppLogin,
	wxLogin
	// getPhoneNumber
}
